﻿using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Configuration;
using System.Web.Configuration;
using System.Web.Security;

namespace Ornament.MVCWebFrame.Areas.MemberShips.Models
{
    public class OrnamentMemberShipSetting
    {
        //default:获取一个值，该值指示在成员资格数据存储区中存储密码的格式。Hashed 可选有：Hashed Encrypted ClearRole,        

        /// <summary>
        /// :user this Provider applicationName,default is host name
        /// </summary>
        [Required]
        [DisplayName("应用程序名")]
        public string ApplicationName { get; set; }

        [DisplayName("密码最大尝试次数")]
        public int MaxInvalidPasswordAttempts { get; set; }

        [DisplayName("PasswordAttemptWindow")]
        [Range(0, 999)]
        public int PasswordAttemptWindow { get; set; }

        [Range(0, 3)]
        [DisplayName("密码中字母最少数目")]
        public int MinRequiredNonAlphanumericCharacters { get; set; }

        [Range(0, 3)]
        [DisplayName("密码最少长度")]
        public int MinRequiredPasswordLength { get; set; }

        [Required]
        [DefaultSettingValue("\\d*")]
        [DisplayName("密码符合的正则表达式")]
        public string PasswordStrengthRegularExpression { get; set; }

        /// <summary>
        /// 指示成员资格提供程序是否配置为允许用户重置其密码。
        /// </summary>
        [DisplayName("是否提供密码重置")]
        public bool EnablePasswordReset { get; set; }

        [DisplayName("是否提供密码取回方案")]
        public bool EnablePasswordRetrieval { get; set; }

        [DisplayName("是否需要密码问题集答案")]
        public bool RequiresQuestionAndAnswer { get; set; }

        [DisplayName("是否提供唯一Emial注册")]
        public bool RequiresUniqueEmail { get; set; }

        [DisplayName("密码保存方式")]
        public MembershipPasswordFormat PasswordFormat { get; set; }

        public static OrnamentMemberShipSetting GetMemberShipSetting()
        {
            MembershipProvider mp = Membership.Provider;
            var result = new OrnamentMemberShipSetting
                             {
                                 ApplicationName = mp.ApplicationName,
                                 EnablePasswordReset = mp.EnablePasswordReset,
                                 EnablePasswordRetrieval = mp.EnablePasswordRetrieval,
                                 MaxInvalidPasswordAttempts = mp.MaxInvalidPasswordAttempts,
                                 MinRequiredNonAlphanumericCharacters = mp.MinRequiredNonAlphanumericCharacters,
                                 MinRequiredPasswordLength = mp.MinRequiredPasswordLength,
                                 PasswordAttemptWindow = mp.PasswordAttemptWindow,
                                 PasswordFormat = mp.PasswordFormat,
                                 PasswordStrengthRegularExpression = mp.PasswordStrengthRegularExpression,
                                 RequiresQuestionAndAnswer = mp.RequiresQuestionAndAnswer,
                                 RequiresUniqueEmail = mp.RequiresUniqueEmail
                             };
            return result;
        }

        public static void Save(OrnamentMemberShipSetting mp)
        {
            Configuration config = WebConfigurationManager.OpenWebConfiguration("~");
            var memberShip = (MembershipSection) config.GetSection("system.web/membership");
            string name = memberShip.DefaultProvider;
            ProviderSettings s = memberShip.Providers[name];
            s.Parameters.Clear();
            s.Parameters.Add("applicationName", mp.ApplicationName);
            s.Parameters.Add("enablePasswordReset", mp.EnablePasswordReset.ToString());
            s.Parameters.Add("enablePasswordRetrieval", mp.EnablePasswordRetrieval.ToString());
            s.Parameters.Add("maxInvalidPasswordAttempts", mp.MaxInvalidPasswordAttempts.ToString());
            s.Parameters.Add("minRequiredNonAlphanumericCharacters", mp.MinRequiredNonAlphanumericCharacters.ToString());
            s.Parameters.Add("minRequiredPasswordLength", mp.MinRequiredPasswordLength.ToString());
            s.Parameters.Add("passwordAttemptWindow", mp.PasswordAttemptWindow.ToString());
            s.Parameters.Add("passwordFormat", mp.PasswordFormat.ToString());
            s.Parameters.Add("passwordStrengthRegularExpression", mp.PasswordStrengthRegularExpression);
            s.Parameters.Add("requiresQuestionAndAnswer", mp.RequiresQuestionAndAnswer.ToString());
            s.Parameters.Add("requiresUniqueEmail", mp.RequiresUniqueEmail.ToString());
            config.Save();
        }
    }
}